package cn.itcast.aop;

import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
@SuppressWarnings("all")
public class StaticHtmlAdvice {

    @Autowired
    private SetmealService setmealService;


    //    @Pointcut(value = "execution(* *.SetmealServiceImpl.deleteById(..)) && execution(* *.SetmealServiceImpl.add(..)) && execution(* *.SetmealServiceImpl.update(..))")
    @Pointcut(value = "execution(* *.*.*.*.SetmealServiceImpl.update(..))||execution(* *.*.*.*.SetmealServiceImpl.add(..))||execution(* *.*.*.*.SetmealServiceImpl.deleteById(..))")
    public void pt() {
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object proceed = pjp.proceed();
        try {
            Integer id = null;
            String methodName = pjp.getSignature().getName();
            System.out.println("当前正在进行" + methodName + "方法,执行aop方法生成静态页面");
            Object[] args = pjp.getArgs();
            if (methodName.equals("add")) {
                Setmeal setmeal = (Setmeal) args[0];
                id = setmeal.getId();
                //生成对应的html文件
                System.out.println("生成对应的html页面" + id);
                setmealService.createStaticHtml(id);
            }
            if (methodName.equals("update")) {
                Setmeal setmeal = (Setmeal) args[0];
                //删除对应的html文件
                // //生成对应的html文件
                System.out.println("删除对应的html文件" + setmeal);
                System.out.println("生成对应的html页面" + setmeal);
                setmealService.createStaticHtml(setmeal.getId());
            }
            if (methodName.equals("deleteById")) {
                id = (Integer) args[0];
                //删除对应的html文件
                System.out.println("删除对应的html文件" + id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proceed;
    }


}